﻿using System;
using System.Collections;
using System.Data.SQLite;
using CSharp.Libs.Koc.ItemClasses;

namespace CSharp.Libs.Koc
{
    public partial class DatabaseOperations
    {
        #region private functions
        /// <summary>_ServerActiveItemExists</summary>
        /// <param name="ServerNumber"></param>
        /// <returns></returns>
        /// <history>created  23.11.2011</history>
        private Boolean _ServerActiveServerNumberExists(
            Int64 ServerNumber)
        {
            try
            {
                String strCMD = String.Concat(
                    "SELECT * FROM tblServerActive WHERE ServerNumber=",
                    ServerNumber.ToString());
                SQLiteCommand CMD = new SQLiteCommand(strCMD, _CON);

                SQLiteDataReader DR = CMD.ExecuteReader(System.Data.CommandBehavior.Default);

                if (DR.HasRows)
                {
                    DR.Close();
                    return true;
                }

                DR.Close();
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }

            return false;
        }

        /// <summary>_ServerActiveGetItems</summary>
        /// <returns></returns>
        /// <history>created  23.11.2011</history>
        private ArrayList _ServerActiveGetItems()
        {
            try
            {
                ArrayList alItems = new ArrayList();
                String strCMD = "SELECT * FROM tblServerActive";
                SQLiteCommand CMD = new SQLiteCommand(strCMD, _CON);

                using (SQLiteDataReader DR = CMD.ExecuteReader(System.Data.CommandBehavior.Default))
                {
                    if (!DR.HasRows)
                    {
                        return null;
                    }

                    while (DR.Read())
                    {
                        KocServerActiveItem KSAI = new KocServerActiveItem()
                        {
                            ID = _GetInt64(DR["ServerActiveID"]),
                            ServerNumber = _GetInt64(DR["ServerNumber"]),
                            ServerActive = _GetBoolean(DR["ServerActive"])
                        };

                        alItems.Add(KSAI);
                    }

                    return alItems;
                }
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }

            return null;
        }

        /// <summary>_ServerActiveCreateItem</summary>
        /// <param name="ServerNumber"></param>
        /// <param name="ServerActive"></param>
        /// <returns></returns>
        /// <history>created  23.11.2011</history>
        private Boolean _ServerActiveCreateItem(
            Int64 ServerNumber,
            Boolean ServerActive)
        {
            try
            {
                String strCMD = String.Concat(
                    "INSERT INTO tblServerActive(",
                    "ServerNumber, ",
                    "ServerActive) VALUES (",
                    ServerNumber.ToString(), ", ",
                    _SetBoolean(ServerActive).ToString(), ")");

                SQLiteCommand CMD = new SQLiteCommand(strCMD, _CON);
                
                Int32 Count = CMD.ExecuteNonQuery();

                if (Count > 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }

            return false;
        }

        /// <summary>_ServerActiveUpdateItem</summary>
        /// <param name="ID"></param>
        /// <param name="ServerNumber"></param>
        /// <param name="ServerActive"></param>
        /// <returns></returns>
        /// <history>created  23.11.2011</history>
        private Boolean _ServerActiveUpdateItem(
            Int64 ID,
            Int64 ServerNmber,
            Boolean ServerActive)
        {
            try
            {
                if (ID > 0 &&
                    ServerNmber > 0)
                {
                    String strCMD = String.Concat(
                        "UPDATE tblServerActive ",
                        "SET ",
                        "ServerActive=", _SetBoolean(ServerActive).ToString(), ", ",
                        "ServerNumber=", ServerNmber.ToString(), " ",
                        "WHERE ServerActiveID=", ID.ToString());

                    SQLiteCommand CMD = new SQLiteCommand(strCMD, _CON);

                    Int32 Count = CMD.ExecuteNonQuery();

                    if (Count > 0)
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }

            return false;
        }

        /// <summary>_ServerActiveDeleteItem</summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        /// <history>created  23.11.2011</history>
        private Boolean _ServerActiveDeleteItem(
            Int64 ID)
        {
            try
            {
                if (ID > 0)
                {
                    String strCMD = String.Concat(
                        "DELETE ",
                        "FROM tblServerActive ",
                        "WHERE ServerActiveID=", ID.ToString());

                    SQLiteCommand CMD = new SQLiteCommand(strCMD, _CON);

                    Int32 Count = CMD.ExecuteNonQuery();

                    if (Count > 0)
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }

            return false;
        }
        #endregion



        #region interface
        #region properties
        #endregion

        #region methods
        public Boolean ServerActiveServerNumberExists(
            Int64 ServerNumber)
        {
            return _ServerActiveServerNumberExists(ServerNumber);
        }

        public ArrayList ServerActiveGetItems()
        {
            return _ServerActiveGetItems();
        }

        public Boolean ServerActiveCreateItem(
            Int64 ServerNumber,
            Boolean ServerActive)
        {
            return _ServerActiveCreateItem(ServerNumber, ServerActive);
        }

        public Boolean ServerActiveUpdateItem(
            Int64 ID,
            Int64 ServerNmber,
            Boolean ServerActive)
        {
            return _ServerActiveUpdateItem(ID, ServerNmber, ServerActive);
        }

        public Boolean ServerActiveDeleteItem(
            Int64 ID)
        {
            return _ServerActiveDeleteItem(ID);
        }
        #endregion
        #endregion
    }
}
